package code.lewdy.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JdbcTemple
{
    private static boolean init = false;

    private static Connection conn = null;
    private static String driver = DBConfigUtil.getDriver();
    private static String url = DBConfigUtil.getUrl();
    private static String user = DBConfigUtil.getUser();
    private static String password = DBConfigUtil.getPassword();
    
    /**获取连接
     * @return
     * @author liuding,2012-5-8
     * synchronized
     */
    public static final Connection getConnection()
    {
        if (!init)
            init();
        try
        {
            conn = DriverManager.getConnection(url, user, password);
        }
        catch (SQLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(conn == null){
        	init();
        	return getConnection();
        }
        return conn;
    }
    
    /**执行一条更新的sql语句
     * @param sql
     * @return
     * @author liuding,2012-5-8
     */
    public static int executeUpdate(String sql){
        int results = 0;
        Connection conn1 = getConnection();
        PreparedStatement pstmt = null;
        try
        {
            pstmt = conn1.prepareStatement(sql);
            results = pstmt.executeUpdate();
        }
        catch (SQLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        close(conn, pstmt, null);
        System.out.println("JdbcTemp.update():"+sql+"=>"+results);
        return results;
    }
    
    /**查询并返回ResultSet
     * @param sql
     * @return
     */
    public static ResultSet executeQuery(Connection conn1, PreparedStatement pstmt, 
    		ResultSet rs, String sql){
        try
        {
            pstmt = conn1.prepareStatement(sql);
            rs = pstmt.executeQuery();
        }
        catch (SQLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return rs;
    }
    
    private static void init()
    {
        driver = DBConfigUtil.getDriver();
        url = DBConfigUtil.getUrl();
        user = DBConfigUtil.getUser();
        password = DBConfigUtil.getPassword();
        System.out.println("driver:"+driver);
        try
        {
            Class.forName(driver);
        }
        catch (ClassNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        init = true; // 初始化完毕
    }

    public static void close(Connection conn, PreparedStatement pstmt, ResultSet rs)
    {
        if (conn != null)
            try
            {
                conn.close();
            }
            catch (SQLException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        if (pstmt != null)
            try
            {
                pstmt.close();
            }
            catch (SQLException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        if (rs != null)
            try
            {
                rs.close();
            }
            catch (SQLException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
}
